<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>移除数组中重复的值</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.array-uintersect.html">array_uintersect</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.array-unshift.html">array_unshift</a></div>
 <div class="up"><a href="ref.array.html">数组 函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="function.array-unique" class="refentry">
   <div class="refnamediv">
    <h1 class="refname">array_unique</h1>
    <p class="verinfo">(PHP 4 &gt;= 4.0.1, PHP 5)</p><p class="refpurpose"><span class="refname">array_unique</span> &mdash; <span class="dc-title">移除数组中重复的值</span></p>

   </div>
 <div class="refsect1 description" id="refsect1-function.array-unique-description">
  <h3 class="title">说明</h3>
     <div class="methodsynopsis dc-description">
      <span class="type">array</span> <span class="methodname"><strong>array_unique</strong></span>
       ( <span class="methodparam"><span class="type">array</span> <code class="parameter">$array</code></span>
    [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$sort_flags</code><span class="initializer"> = SORT_STRING</span></span>
     ] )</div>

    <p class="para rdfs-comment">
     <span class="function"><strong>array_unique()</strong></span> 接受
     <em><code class="parameter">array</code></em> 作为输入并返回没有重复值的新数组。
    </p>
    <p class="para">
     注意键名保留不变。<span class="function"><strong>array_unique()</strong></span>
     先将值作为字符串排序，然后对每个值只保留第一个遇到的键名，接着忽略所有后面的键名。这并不意味着在未排序的
     <em><code class="parameter">array</code></em> 中同一个值的第一个出现的键名会被保留。
    </p>
    <blockquote class="note"><p><strong class="note">Note</strong>: 
     <span class="simpara">
      当且仅当
      <em>(string) $elem1 === (string) $elem2</em> 时两个单元被认为相同。就是说，当字符串的表达一样时。
     </span>
     <span class="simpara">
      第一个单元将被保留。
     </span>
    </p></blockquote>
   </div>


 <div class="refsect1 parameters" id="refsect1-function.array-unique-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<em><code class="parameter">array</code></em></dt>

     <dd>

      <p class="para">
       输入的数组。
      </p>
     </dd>

    
    
     <dt>
<em><code class="parameter">sort_flags</code></em></dt>

     <dd>

      <p class="para">
       The optional second parameter <em><code class="parameter">sort_flags</code></em>
       may be used to modify the sorting behavior using these values:
      </p>
      <p class="para">
       Sorting type flags:
       <ul class="itemizedlist">
        <li class="listitem">
         <span class="simpara"><strong><code>SORT_REGULAR</code></strong> - compare items normally
         (don&#039;t change types)</span>
        </li>
        <li class="listitem">
         <span class="simpara"><strong><code>SORT_NUMERIC</code></strong> - compare items numerically</span>
        </li>
        <li class="listitem">
         <span class="simpara"><strong><code>SORT_STRING</code></strong> - compare items as strings</span>
        </li>
        <li class="listitem">
         <span class="simpara"><strong><code>SORT_LOCALE_STRING</code></strong> - compare items as
          strings, based on the current locale.
          
         </span>
        </li>
       </ul>
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.array-unique-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   Returns the filtered array.
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.array-unique-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>5.2.10</td>
       <td>
        Changed the default value of <em><code class="parameter">sort_flags</code></em> back to
        <strong><code>SORT_STRING</code></strong>.
       </td>
      </tr>

      <tr>
       <td>5.2.9</td>
       <td>
        Added the optional <em><code class="parameter">sort_flags</code></em> defaulting
        to <strong><code>SORT_REGULAR</code></strong>. Prior to 5.2.9, this function used to sort
        the array with <strong><code>SORT_STRING</code></strong> internally.
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.array-unique-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-4894">
    <p><strong>Example #1 <span class="function"><strong>array_unique()</strong></span> 例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$input&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #DD0000">"a"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"green"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"red"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"b"&nbsp;</span><span style="color: #007700">=&gt;&nbsp;</span><span style="color: #DD0000">"green"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"blue"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"red"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">array_unique</span><span style="color: #007700">(</span><span style="color: #0000BB">$input</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
Array
(
    [a] =&gt; green
    [0] =&gt; red
    [1] =&gt; blue
)
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   <div class="example" id="example-4895">
    <p><strong>Example #2 <span class="function"><strong>array_unique()</strong></span> 和类型</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$input&nbsp;</span><span style="color: #007700">=&nbsp;array(</span><span style="color: #0000BB">4</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"4"</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"3"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">4</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"3"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$result&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">array_unique</span><span style="color: #007700">(</span><span style="color: #0000BB">$input</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
array(2) {
  [0] =&gt; int(4)
  [2] =&gt; string(1) &quot;3&quot;
}
</pre></div>
    </div>
   </div>
  </p>
 </div>

 
 <div class="refsect1 seealso" id="refsect1-function.array-unique-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.array-count-values.html" class="function" rel="rdfs-seeAlso">array_count_values()</a> - 统计数组中所有的值出现的次数</span></li>
   </ul>
  </p>
 </div>


 <div class="refsect1 notes" id="refsect1-function.array-unique-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <span class="simpara">
    Note that <span class="function"><strong>array_unique()</strong></span> is not intended to 
    work on multi dimensional arrays.
   </span>
  </p></blockquote>
 </div>

  </div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.array-uintersect.html">array_uintersect</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.array-unshift.html">array_unshift</a></div>
 <div class="up"><a href="ref.array.html">数组 函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
